﻿
//#region 弹出层
//{html:'', title:'', width:100, height:90}
function popwindows(dialogobj) {
    var dialog = {
        dialog_mask: 'dialog_bg',
        dialog_id: 'dialog_id',
        dialog_close: 'dialog_close',
        init: function () {
            $.extend(dialog, dialogobj);
            dialog.checkId();
            dialog.createdialog();
        },
        checkId: function () {
            if ($("#" + dialog.dialog_id).length > 0) {
                $("#" + dialog.dialog_mask, "#" + dialog.dialog_id).remove();
            }
        },
        createdialog: function () {
            var _dialoghtml = [];
            _dialoghtml.push('<div id="dialog_bg" style="background:#000;width:100%;height:100%;position:fixed; _position:absolute; top:0;left:0; z-index: 20000;filter:alpha(opacity=50);-moz-opacity:0.5;-khtml-opacity: 0.5;opacity: 0.5; "></div>');
            _dialoghtml.push('<div id="' + dialog.dialog_id + '" style="background:#fff;position: absolute; z-index: 20001;border:1px solid #ccc;">');
            _dialoghtml.push('<div style="background:#d5d5d5;height:24px;line-height:24px;padding:0 5px;"><span style="width:30px;float:right;cursor:pointer" title="关闭" id="dialog_close">关闭</span><span>');
            _dialoghtml.push(dialog.title);
            _dialoghtml.push('</span></div><div>');
            _dialoghtml.push(dialog.html);
            _dialoghtml.push('</div><div style="height:1px;clear:both;"></div></div>');
            $('body').append(_dialoghtml.join(''));

            $('#' + dialog.dialog_close).bind('click', function () {
                dialog.close();
            });

            $("#" + dialog.dialog_id).css({ width: dialog.width + "px" });
            if (dialog.height) { $("#" + dialog.dialog_id).css({ height: dialog.height + "px" }); }

            $("#" + dialog.dialog_id).css({ left: ($(window).width() - $("#" + dialog.dialog_id).width()) / 2 + $(window).scrollLeft() + "px",
                top: ($(window).height() - $("#" + dialog.dialog_id).height()) / 2 + $(window).scrollTop() + "px"
            });
        },
        close: function () {
            $("#" + dialog.dialog_mask + ",#" + dialog.dialog_id).remove();
        }
    }
    dialog.init();
    return dialog;
}
//#endregion
var dataCache = [
                { "label": "塔里木油田" },
                { "label": "大庆油田" },
                { "label": "采油厂" },
                { "label": "八厂" },
                { "label": "井123" },
                { "label": "你必须知道的.NET" }
            ];

var dialog_opendiv;
function searchtype(type,inputId) {
    var hideInputId = '';
    var typearr = [lang.Object_list, lang.Business_list];
    if (type == 0) hideInputId = "targetValue";
    else
        hideInputId = "bizValue";
    $('#' + hideInputId).val('');

    var _html = [];
    if (type == 0) {

        _html.push('<table id="tt" style="height:390px; overflow:auto " rownumbers="true" idField="id" treeField="name">');
        _html.push('  <thead>');
        _html.push('   <tr>');
        _html.push('  <th field="name" width="650"></th>');
        _html.push('  </tr>');
        _html.push('</thead>');
        _html.push('</table>');

        dialog_opendiv = popwindows({ html: _html.join(''), title: typearr[type], width: 500, height: 420 });

        $('#tt').treegrid({
            url: '../../Handlers/getTargetNodes.aspx',

            onDblClickCell: function (field, row) { if (!isexist(inputId, hideInputId, row.id)) { $('#' + inputId).val($('#' + inputId).val() + " " + row.name); } }

        }

     );

    }
    else {
        _html.push('<div>')
        _html.push('<div style="overflow:auto;height:390px;">')
        _html.push('<div id="tree">');
        _html.push('</div>');
        _html.push('</div>');
        //_html.push('<div style="border-top:1px solid #ccc;margin:0 atuo;padding:15px 0 0 0;text-align:center;"><span style="padding:5px;border:1px solid #eee;background:#f8f8f8;margin:0 30px;cursor:pointer;" onclick="clearselect(' + type + ')">清空选择</span><span  style="padding:5px;border:1px solid #eee;background:#f8f8f8;cursor:pointer;"  onclick="selectnode(' + type + ')">确定选择</span></div>')
        _html.push('</div>');

        dialog_opendiv = popwindows({ html: _html.join(''), title: typearr[type], width: 500, height: 420 });

        $('#tree').tree({ 'checkbox': false, onDblClick: function (node) { if (!isexist(inputId, hideInputId, node.id)) { $('#' + inputId).val($('#' + inputId).val() + " " + node.text); } } });
        gettreelist(type);
    }
}

//确定选择节点
function selectnode(type) {
    var nodes = $('#tree').tree('getChecked')
    var _selectnode = [];
    var _selectnodeText = [];
    for (var i = 0; i < nodes.length; i++) {
        _selectnode.push(nodes[i].id);
        _selectnodeText.push(nodes[i].text);
    }
    var idstr = '';
    var textstr = '';
    if (type == 0) {
        idstr = "targetValue";
        textstr = "txtMB";
    }
    else {
        idstr = "bizValue";
        textstr = "txtYWDX";
    }
    $("#" + idstr).val(_selectnode.join());
    $("#" + textstr).val(_selectnode.join());
    $("#" + textstr).val(_selectnodeText.join());
    dialog_opendiv.close();

}

//清空节点
function clearselect(type) {
    if (type == 0)
        idstr = "targetValue";
    else
        idstr = "bizValue";
    $("#" + idstr).val('');
    dialog_opendiv.close();
}

//#region 加载菜单数据
function gettreelist(typenum) {
    $.ajaxHandler({
        url: App_Config.TadongUrl + "/AjaxMappingHandler.aspx",
        urlType: "QueryAllTree",
        model: function () { return typenum; },
        success: function (data) {
            if (data && data.length > 0) {
                var _data = [];
                for (var i = 0; i < data.length; i++) {
                    if (data[i].PObjectID == '0') {
                        var obj = {};
                        obj.id = data[i].ObjectID;
                        obj.text = data[i].ObjectName;
                        getsubtree(data, data[i].ObjectID, obj);
                        _data.push(obj);
                    }
                }
                $('#tree').tree('loadData', _data);
                //绑定选中
                if (typenum == 0)
                    idstr = "targetValue";
                else
                    idstr = "bizValue";
            }

        }
    });
    //6b5.testdsssfes15s541a12s
    function getsubtree(_d, _pid, obj2) {
        if (_d && _d.length > 0) {
            var _sdata = [];
            for (var i = 0; i < _d.length; i++) {
                if (_d[i].PObjectID == _pid) {
                    var obj = {};
                    obj.id = _d[i].ObjectID;
                    obj.text = _d[i].ObjectName;
                    _sdata.push(obj);
                }
            }

            obj2.children = _sdata;
            if (_sdata.length > 0) {
                for (var j = 0; j < _sdata.length; j++) {
                    getsubtree(_d, _sdata[j].id, _sdata[j]);
                }
            }
        }
    }
}


function showTargetTree() {

    searchtype(0,'txtMB');
}
function showBizTree() {
    searchtype(1,'txtYWDX');
}




